package cn.team.controller;

import cn.team.entity.Role;
import cn.team.entity.User;
import cn.team.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@RequestMapping("/user")
@RestController
public class UserController {
    @Resource
    JavaMailSender mailSender;
    @Resource
    UserService userService;
    @Resource
    PasswordEncoder encoder;

    @GetMapping("/mail")
    public void getMailMessage(){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("hhh");
        mailMessage.setText("wwww");
        mailMessage.setFrom("3331885126@qq.com");
        mailMessage.setTo("3337035399@qq.com");
        mailSender.send(mailMessage);
    }

    @GetMapping("/register")
    public Map<String, String> register(HttpServletResponse response,
                                        @RequestParam("username") String username, @RequestParam("password") String password) {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        HashMap<String, String> map = new HashMap<>();
        User user = userService.getUser(username);
        if (user != null) {
            map.put("msg", "此账号已经注册");
            return map;
        }
        int result = userService.saveUser(username, password, FileController.getDefaultPortrait());
        System.out.println("用户保存---" + result);
        map.put("msg", "ok");
        return map;
    }
    @GetMapping("/updatePassword")
    public Map<String, String> updatePassword(HttpServletResponse response,
                                        @RequestParam("username") String username, @RequestParam("password") String password,
                                              @RequestParam("rpassword") String rpassword) {
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        HashMap<String, String> map = new HashMap<>();
        User user = userService.getUser(username);
        if (encoder.matches(password,user.getPassword())) {
            userService.updatePassword(username, encoder.encode(rpassword));
            map.put("msg", "ok");
            return map;

        } else {
            map.put("msg", "此账号已经注册");
            return map;
        }


    }

    @GetMapping("/user/{username}")
    public HashMap<String, String> user(@PathVariable("username") String username, HttpServletResponse response, HttpSession session) {
        User user = userService.getUser(username);
        HashMap<String, String> map = new HashMap();
        if (user != null) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "err");
        }
        return map;
    }



    @GetMapping("/searchUser/{username}")
    public User selectUser(@PathVariable("username") String username, HttpSession session, HttpServletResponse response) {
//        response.setContentType(MediaType.APPLICATION_JSON_VALUE);


        User user = userService.getUser(username);
        List<Role> roles = new ArrayList<>();
        roles.add(new Role(1, "rommen", "rommen"));

        user.setRoles(roles);
        System.out.println("load user " + user);
        return user;

    }

    @PostMapping
    public HashMap<String, String> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.getUser(username);
        HashMap<String, String> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }

    @GetMapping("/updateUser")
    public int updateUser(@RequestParam("username")String username,@RequestParam("petName")String petName,
                           @RequestParam("email")String email,@RequestParam("phone")String phone,
                           @RequestParam("gender")String gender,@RequestParam("age")int age){
        System.out.println("update---------------user");
         return userService.updateUser(username,petName,email,phone,gender,age);
    }
}
